import 'dart:convert' show json;

import 'package:flutter/cupertino.dart';

import 'base_chart_axis.dart';
import 'base_chart_formatter.dart';
import 'base_model_asT.dart';
import 'package:ribo_base_core/base_styles/base_chart_style.dart';

class BaseChartDumbbellsModel {
  /// 拐点颜色列表，顺序和dumbbellsData里的顺序对应
  List<String> colors;

  /// 线条颜色
  String lineColor;

  /// https://echarts.apache.org/zh/option.html#tooltip.formatter
  BaseChartFormatter tooltipFormatter;

  /// https://echarts.apache.org/zh/option.html#series-pie.label.formatter
  BaseChartFormatter labelFormatter;

  /// 坐标轴显示设置
  BaseChartAxis axis;

  /// 图表样式
  BaseChartStyle style;

  /// 图表数据
  List<BaseChartDumbbellsData> dumbbellsData;

  /// https://echarts.apache.org/zh/option.html#dataZoom
  Map<String, dynamic> dataZoom;

  BaseChartDumbbellsModel({
    this.tooltipFormatter,
    this.labelFormatter,
    this.style,
    this.axis,
    this.colors,
    this.lineColor,
    this.dataZoom,
    @required this.dumbbellsData,
  });

  factory BaseChartDumbbellsModel.fromJson(Map<String, dynamic> jsonRes) {
    if (jsonRes == null) {
      return null;
    }

    final List<BaseChartDumbbellsData> dumbbellsData =
        jsonRes['dumbbellsData'] is List ? <BaseChartDumbbellsData>[] : null;
    if (dumbbellsData != null) {
      for (final dynamic item in jsonRes['dumbbellsData']) {
        if (item != null) {
          dumbbellsData.add(BaseChartDumbbellsData.fromJson(
              asT<List<Map<String, dynamic>>>(item)));
        }
      }
    }
    return BaseChartDumbbellsModel(
      tooltipFormatter: asT<BaseChartFormatter>(jsonRes['tooltipFormatter']),
      labelFormatter: asT<BaseChartFormatter>(jsonRes['labelFormatter']),
      axis: asT<BaseChartAxis>(jsonRes['axis']),
      style: asT<BaseChartStyle>(jsonRes['style']),
      colors: asT<List<String>>(jsonRes['colors']),
      lineColor: asT<String>(jsonRes['lineColor']),
      dataZoom: asT<Map<String, dynamic>>(jsonRes['dataZoom']),
      dumbbellsData: dumbbellsData,
    );
  }

  Map<String, dynamic> toJson() => <String, dynamic>{
        'tooltipFormatter': tooltipFormatter,
        'labelFormatter': labelFormatter,
        'axis': axis,
        'style': style,
        'colors': colors,
        'lineColor': lineColor,
        'dataZoom': dataZoom,
        'dumbbellsData': dumbbellsData,
      };

  @override
  String toString() {
    return json.encode(this);
  }
}

class BaseChartDumbbellsData {
  List<BaseChartLineData> datas;

  BaseChartDumbbellsData(List<BaseChartLineData> datas) : this.datas = datas;

  factory BaseChartDumbbellsData.fromJson(List<Map<String, dynamic>> jsonRes) =>
      jsonRes == null
          ? null
          : BaseChartDumbbellsData(asT<List<BaseChartLineData>>(jsonRes));

  List<Map<String, dynamic>> toJson() => datas.map((e) => e.toJson()).toList();

  @override
  String toString() {
    return json.encode(this);
  }
}


class BaseChartLineData {
  BaseChartLineData({String name, this.value}):this.name = name.startsWith("20")?name.replaceRange(0, 2, ""):name; //只显示最后的日期

  factory BaseChartLineData.fromJson(Map<String, dynamic> jsonRes) =>
      jsonRes == null
          ? null
          : BaseChartLineData(
        name: asT<String>(jsonRes['name']),
        value: asT<double>(jsonRes['value']),
      );

  /// 数据名称
  String name;

  /// 数据值
  double value;

  Map<String, dynamic> toJson() =>
      <String, dynamic>{'name': name, 'value': value};

  @override
  String toString() {
    return json.encode(this);
  }
}
